﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class SysCodeDA : ISysCodeDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCRIPTION = "@DESCRIPTION";
        private string SQL_CHECK_SYS_CODE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@SYS_CODE WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE ";
        private string SQL_DELETE_SYS_CODE = " DELETE FROM @_@SYS_CODE WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE  AND SYSTEM_CREATED = 'N' ";
        private string SQL_INSERT_SYS_CODE = " INSERT INTO @_@SYS_CODE ( WH_ID, SYS_CODE_TYPE, DESCRIPTION, SYSTEM_CREATED, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @SYS_CODE_TYPE, @DESCRIPTION, @SYSTEM_CREATED, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_SYS_CODE = " SELECT  SC.ROWID, SC.WH_ID, SC.SYS_CODE_TYPE, SC.DESCRIPTION, T.[TEXT] AS DESCRIPTION_D, SC.SYSTEM_CREATED, SC.CREATED_BY, SC.CREATED_DATE, SC.UPDATED_BY, SC.UPDATED_DATE   FROM  @_@SYS_CODE SC LEFT JOIN sysadm.TRANSLATION T ON SC.SYS_CODE_TYPE = T.ITEM_ID  AND  T.ITEM_TYPE = 'SYS_CODE' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_SYS_CODE = " UPDATE @_@SYS_CODE SET WH_ID = @WH_ID, DESCRIPTION = @DESCRIPTION, SYSTEM_CREATED = @SYSTEM_CREATED, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE  ";
        private string SYS_CODE_TYPE = "@SYS_CODE_TYPE";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckSysCodeIDUnique(string sysCodeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_SYS_CODE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckSysCodeIDUnique(DataBase dataBase, DbTransaction tran, string sysCodeID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_SYS_CODE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteSysCode(List<string> sysCodeID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysCodeID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_SYS_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteSysCode(DataBase dataBase, DbTransaction tran, List<string> sysCodeID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < sysCodeID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", sysCodeID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_SYS_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllSysCode(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllSysCode(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public SysCodeInfo GetSysCodeByID(string syscodeID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE.Replace("@_@", whLoginID).Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'") + " WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", syscodeID) };
            SysCodeInfo sysCodeInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    sysCodeInfo = new SysCodeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_CODE_TYPE"].ToString(), reader["DESCRIPTION"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return sysCodeInfo;
        }

        public SysCodeInfo GetSysCodeByID(DataBase dataBase, DbTransaction tran, string syscodeID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_SYS_CODE.Replace("@_@", whLoginID).Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'") + " WHERE SYS_CODE_TYPE = @SYS_CODE_TYPE  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@SYS_CODE_TYPE", syscodeID) };
            SysCodeInfo sysCodeInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                sysCodeInfo = new SysCodeInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["SYS_CODE_TYPE"].ToString(), reader["DESCRIPTION"].ToString(), reader["SYSTEM_CREATED"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return sysCodeInfo;
        }

        public DataSet GetSysCodeByQueryList(List<string> sqlWhere, SysCodeQueryEntity syscodeQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_SYS_CODE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!syscodeQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, syscodeQuery.CurrentPage, syscodeQuery.PageSize, syscodeQuery.SortField, syscodeQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetSysCodeByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, SysCodeQueryEntity syscodeQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_SYS_CODE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!syscodeQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, syscodeQuery.CurrentPage, syscodeQuery.PageSize, syscodeQuery.SortField, syscodeQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            else
            {
                temp = temp.Replace("@LANGUAGE", "'" + pubParasInfo.UserLanguage + "'");
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertSysCode(SysCodeInfo sysCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysCode_Parameters(sysCodeInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_SYS_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertSysCode(DataBase dataBase, DbTransaction tran, SysCodeInfo sysCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysCode_Parameters(sysCodeInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_SYS_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_SysCode_Parameters(SysCodeInfo sysCodeInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.SYS_CODE_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.DESCRIPTION, SqlDbType.VarChar, 100), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 0x12), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 0x12) };
            if (!string.IsNullOrEmpty(sysCodeInfo.WhID))
            {
                paramArray[0].Value = sysCodeInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = sysCodeInfo.SysCodeType;
            if (!string.IsNullOrEmpty(sysCodeInfo.Description))
            {
                paramArray[2].Value = sysCodeInfo.Description;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = sysCodeInfo.SystemCreated;
            paramArray[4].Value = sysCodeInfo.CreatedBy;
            paramArray[5].Value = sysCodeInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateSysCode(SysCodeInfo sysCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysCode_Parameters(sysCodeInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_SYS_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateSysCode(DataBase dataBase, DbTransaction tran, SysCodeInfo sysCodeInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    sysCodeInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_SysCode_Parameters(sysCodeInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_SYS_CODE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

